/*******************************************************************************
 Jimm - Mobile Messaging - J2ME ICQ clone
 Copyright (C) 2003-05  Jimm Project

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ********************************************************************************
 File: src/jimm/ContactList.java
 Version: ###VERSION###  Date: ###DATE###
 Author(s): Manuel Linsmayer, Andreas Rossbacher, Artyomov Denis
 *******************************************************************************/

package jimm.cl;

import DrawControls.icons.Icon;
import DrawControls.icons.*; //menu icons
import DrawControls.tree.*;
import DrawControls.*;
import javax.microedition.lcdui.TextField;
import jimm.*;
import jimm.chat.*;
import jimm.comm.*;
import jimm.forms.*;
import jimm.modules.*;
import jimm.ui.*;
import jimm.ui.base.NativeCanvas;
import jimm.ui.menu.*;
import java.util.*;
import protocol.*;
import protocol.icq.*;
import protocol.mrim.*;
import protocol.jabber.*;
// #sijapp cond.if protocols_OBIMP is "true" #
import protocol.obimp.*;
// #sijapp cond.end #


public final class ContactList implements TextBoxListener, SelectListener, ContactListListener {
    private static final ContactList instance = new ContactList();
    private final TextListEx aboutTextList = new TextListEx(null);
    private final MenuModel mainMenu = new MenuModel();
    private MessageEditor editor;
    private Select mainMenuView;
    private VirtualContactList contactList;
    private final StatusView statusView = new StatusView();
    private Contact currentContact;
    private Protocol activeProtocol;
    private InputTextBox passwordTextBox;
    // #sijapp cond.if modules_FILES="true"#
    private Vector transfers = new Vector();
    // #sijapp cond.end#

    public ContactList() {
    }
    public void initUI() {
        contactList = new VirtualContactList();
        contactList.setCLListener(this);
        mainMenu.setActionListener(this);
        mainMenuView = new Select(mainMenu);
    }
    public void initMessageEditor() {
        editor = new MessageEditor();
    }

    public byte getProtocolType(Profile account) {
        for (int i = 0; i < Profile.protocolTypes.length; ++i) {
            if (account.protocolType == Profile.protocolTypes[i]) {
                return account.protocolType;
            }
        }
        return Profile.protocolTypes[0];
    }
    // #sijapp cond.if modules_MULTI is "true" #
    private boolean is(Protocol protocol, Profile profile) {
        if (protocol.getProfile() == profile) {
            return true;
        }
        return (protocol.getProfile().protocolType == profile.protocolType)
                && profile.userId.equals(protocol.getProfile().userId);
    }
    public void addProtocols(Vector accounts) {
        int count = contactList.getModel().getProtocolCount();
        Protocol[] protocols = new Protocol[count];
        for (int i = 0; i < count; ++i) {
            protocols[i] = contactList.getModel().getProtocol(i);
        }
        contactList.getModel().removeAllProtocols();
        for (int i = 0; i < accounts.size(); ++i) {
            Profile profile = (Profile)accounts.elementAt(i);
            for (int j = 0; j < protocols.length; ++j) {
                Protocol protocol = protocols[j];
                if ((null != protocol) && is(protocol, profile)) {
                    if (protocol.getProfile() != profile) {
                        protocol.setProfile(profile);
                    }
                    protocols[j] = null;
                    profile = null;
                    contactList.getModel().addProtocol(protocol);
                    break;
                }
            }
            if (null != profile) {
                addProtocol(profile, true);
            }
        }
        for (int i = 0; i < protocols.length; ++i) {
            Protocol protocol = protocols[i];
            if (null != protocol) {
                protocol.disconnect();
                protocol.needSave();
                protocol.dismiss();
            }
        }
    }
    // #sijapp cond.end #
    public void initAccounts() {
        // #sijapp cond.if modules_MULTI is "true" #
        int count = Math.max(1, Options.getAccountCount());
        for (int i = 0; i < count; ++i) {
            Profile p = Options.getAccount(i);
            if (p.isActive) {
                addProtocol(p, false);
            }
        }
        // #sijapp cond.else #
        addProtocol(Options.getAccount(Options.getCurrentAccount()), false);
        // #sijapp cond.end #
    }
    public void loadAccounts() {
        int count = contactList.getModel().getProtocolCount();
        for (int i = 0; i < count; ++i) {
            Protocol protocol = contactList.getModel().getProtocol(i);
            protocol.safeLoad();
        }
    }

    private byte getRealType(byte type) {
        // #sijapp cond.if protocols_JABBER is "true" #
        // #sijapp cond.if modules_MULTI is "true" #
        switch (type) {
            case Profile.PROTOCOL_GTALK:
            case Profile.PROTOCOL_FACEBOOK:
            case Profile.PROTOCOL_LJ:
            case Profile.PROTOCOL_YANDEX:
            case Profile.PROTOCOL_VK:
            case Profile.PROTOCOL_QIP:
            case Profile.PROTOCOL_ODNOKLASSNIKI:
                return Profile.PROTOCOL_JABBER;
        }
        // #sijapp cond.end #
        // #sijapp cond.end #
        return type;
    }
    public void addProtocol(Profile account, boolean load) {
        Protocol protocol = null;
        byte type = getProtocolType(account);
        switch (getRealType(type)) {
            // #sijapp cond.if protocols_ICQ is "true" #
            case Profile.PROTOCOL_ICQ:
                protocol = new Icq();
                break;
            // #sijapp cond.end #
            // #sijapp cond.if protocols_MRIM is "true" #
            case Profile.PROTOCOL_MRIM:
                protocol = new Mrim();
                break;
            // #sijapp cond.end #
            // #sijapp cond.if protocols_JABBER is "true" #
            case Profile.PROTOCOL_JABBER:
                protocol = new Jabber();
                break;
            // #sijapp cond.end #
            // #sijapp cond.if protocols_MSN is "true" #
            case Profile.PROTOCOL_MSN:
                protocol = new protocol.msn.Msn();
                break;
            // #sijapp cond.end #
                // #sijapp cond.if protocols_OBIMP is "true" #
            case Profile.PROTOCOL_OBIMP:
                protocol = new protocol.obimp.Obimp();
                break;
                // #sijapp cond.end #
        }
        if (null == protocol) {
            return;
        }
        protocol.setProfile(account);
        protocol.init();
        if (load) {
            protocol.safeLoad();
        }
        contactList.getModel().addProtocol(protocol);
    }
    public Protocol getProtocol(Profile profile) {
        int count = contactList.getModel().getProtocolCount();
        for (int i = 0; i < count; ++i) {
            Protocol p = contactList.getModel().getProtocol(i);
            if (p.getProfile() == profile) {
                return p;
            }
        }
        return null;
    }

    public Protocol getProtocol(String account) {
        int count = contactList.getModel().getProtocolCount();
        for (int i = 0; i < count; ++i) {
            Protocol p = contactList.getModel().getProtocol(i);
            if (p.getUserId().equals(account)) {
                return p;
            }
        }
        return null;
    }

    public static ContactList getInstance() {
        return instance;
    }
    public Protocol[] getProtocols() {
        ContactListInterface model = contactList.getModel();
        Protocol[] all = new Protocol[model.getProtocolCount()];
        for (int i = 0; i < all.length; ++i) {
            all[i] = model.getProtocol(i);
        }
        return all;
    }

    public Protocol getProtocol(Contact c) {
        ContactListInterface model = contactList.getModel();
        for (int i = 0; i < model.getProtocolCount(); ++i) {
            if (model.getProtocol(i).inContactList(c)) {
                return model.getProtocol(i);
            }
        }
        return null;
    }
    
    public void activate() {
        contactList.setAlwaysVisibleNode(null);
        contactList.update();
        contactList.showTop();
    }
    public void activate(Contact c) {
        contactList.setActiveContact(c);
        contactList.setAlwaysVisibleNode(c);
        contactList.update();
        contactList.showTop();
    }
    
    public void startUp() {
        if (0 == Options.getAccountCount()) {
            updateUnreadedMessageCount();
            contactList.update();
            updateMainMenu();
            mainMenu.setDefaultItemCode(MENU_STATUS);
            Jimm.getJimm().getDisplay().pushWindow(contactList);
            Jimm.getJimm().getDisplay().pushWindow(mainMenuView);
            new AccountsForm().showAccountEditor(null);

        } else {
            contactList.showTop();
            ChatHistory.instance.loadUnreadMessages();
            updateUnreadedMessageCount();
            if (Options.getBoolean(Options.OPTION_AUTO_CONNECT)) {
                autoConnect();
            }
        }
    }
    private void autoConnect() {
        int count = contactList.getModel().getProtocolCount();
        for (int i = 0; i < count; ++i) {
            contactList.getModel().getProtocol(i).connect();
        }
    }
    public void updateAbout() {
        aboutTextList.initAbout();
    }
    
    public MessageEditor getMessageEditor() {
        return editor;
    }

    /* *********************************************************** */
    final static public int SORT_BY_STATUS = 0;
    final static public int SORT_BY_ONLINE = 1;
    final static public int SORT_BY_NAME   = 2;
    
    /* *********************************************************** */
    // #sijapp cond.if modules_FILES="true"#
    public void addTransfer(FileTransfer ft) {
        transfers.addElement(ft);
    }
    public void removeTransfer(DrawControls.text.Par par, boolean cancel) {
        for (int i = 0; i < transfers.size(); ++i) {
            FileTransfer ft = (FileTransfer)transfers.elementAt(i);
            if (ft.is(par)) {
                transfers.removeElementAt(i);
                if (cancel) {
                    ft.cancel();
                }
                return;
            }
        }
    }
    // #sijapp cond.end#

    public boolean isConnected() {
        int count = contactList.getModel().getProtocolCount();
        for (int i = 0; i < count; ++i) {
            Protocol p = contactList.getModel().getProtocol(i);
            if (p.isConnected()) {
                return true;
            }
        }
        return false;
    }
    public void disconnect() {
        int count = contactList.getModel().getProtocolCount();
        for (int i = 0; i < count; ++i) {
            Protocol p = contactList.getModel().getProtocol(i);
            if (p.isConnected()) {
                p.disconnect();
            }
        }
    }
    public void safeSave() {
        int count = contactList.getModel().getProtocolCount();
        for (int i = 0; i < count; ++i) {
            Protocol p = contactList.getModel().getProtocol(i);
            p.safeSave();
        }
    }

    public void collapseAll() {
        int count = contactList.getModel().getProtocolCount();
        for (int i = 0; i < count; ++i) {
            Protocol p = contactList.getModel().getProtocol(i);
            Vector groups = p.getGroupItems();
            for (int groupIndex = 0; groupIndex < groups.size(); ++groupIndex) {
                ((TreeBranch)groups.elementAt(groupIndex)).setExpandFlag(false);
            }
            p.getNotInListGroup().setExpandFlag(false);
        }
        contactList.setCurrentItem(0);
        contactList.update();
    }
    public VirtualContactList getManager() {
        return contactList;
    }
    
    /**
     * Adds the given message to the message queue of the contact item
     * identified by the given UIN
     */
    public void setActiveContact(Contact contact) {
        boolean isShown = (Jimm.getJimm().getDisplay().getCurrentDisplay() == contactList);
        if (isShown && (0 == cursorLock)) {
            contactList.setActiveContact(contact);
        }
    }
    private int cursorLock = 0;
    private int contactListSaveDelay = 0;
    public final void userActivity() {
        cursorLock = 4 /* * 250 = 1 sec */;
        // #sijapp cond.if modules_ABSENCE is "true" #
        jimm.modules.AutoAbsence.instance.userActivity();
        // #sijapp cond.end #
    }
    public final void needRosterSave() {
        contactListSaveDelay = 60 * 4 /* * 250 = 60 sec */;
    }
    public final void timerAction() {
        // #sijapp cond.if modules_ABSENCE is "true" #
        jimm.modules.AutoAbsence.instance.updateTime();
        // #sijapp cond.end #
        if (0 < cursorLock) {
            cursorLock--;
        }
        if (0 < contactListSaveDelay) {
            contactListSaveDelay--;
            if (0 == contactListSaveDelay) {
                int count = contactList.getModel().getProtocolCount();
                for (int i = 0; i < count; ++i) {
                    Protocol p = contactList.getModel().getProtocol(i);
                    p.safeSave();
                }
            }
        }
    }

    public final void recivedMessage(Contact contact) {
        // Notify splash canvas
        if (Jimm.isLocked()) {
            Jimm.getJimm().splash.messageAvailable();
        }
        updateUnreadedMessageCount();
    }
    public final void markMessages(Contact contact) {
        if (null != VirtualList.getMessageIcon()) {
            updateUnreadedMessageCount();
        }
    }
    private void updateUnreadedMessageCount() {
        Icon icon = ChatHistory.instance.getUnreadMessageIcon();
        if (icon != VirtualList.getMessageIcon()) {
            VirtualList.setMessageIcon(icon);
            jimm.ui.base.NativeCanvas.getInstance().repaint();
        }
    }
 
    

    public final Contact getCurrentContact() {
        return currentContact;
    }
    public final void setCurrentContact(Contact contact) {
        currentContact = contact;
    }
    public StatusView getStatusView() {
        return statusView;
    }

    /* Static constants for menu actios */
    private static final int MENU_CONNECT = 1;
    private static final int MENU_DISCONNECT = 2;
    private static final int MENU_DISCO = 3;
    private static final int MENU_OPTIONS = 4;
    private static final int MENU_KEYLOCK = 6;
    private static final int MENU_STATUS = 7;
    private static final int MENU_XSTATUS = 8;
    private static final int MENU_PRIVATE_STATUS = 9;
    private static final int MENU_GROUPS = 10;
    private static final int MENU_SEND_SMS = 11;
    private static final int MENU_ABOUT = 12;
    private static final int MENU_MINIMIZE = 13;
    private static final int MENU_SOUND = 14;
    private static final int MENU_MYSELF = 15;
    private static final int MENU_DEBUGLOG = 17;
    private static final int MENU_MAGIC_EYE = 18;
    private static final int MENU_MICROBLOG = 19;
    private static final int MENU_TEST = 20;
    private static final int MENU_EXIT = 21;

    /////////////////////////////////////////////////////////////////

    /** ************************************************************************* */
    public boolean isCollapsible() {
        return Jimm.isPhone(Jimm.PHONE_SE) || Jimm.isPhone(Jimm.PHONE_NOKIA_S60);
    }
    private boolean isSmsSupported() {
        // #sijapp cond.if protocols_MRIM is "true" #
        int count = contactList.getModel().getProtocolCount();
        for (int i = 0; i < count; ++i) {
            Protocol p = contactList.getModel().getProtocol(i);
            if ((p instanceof Mrim) && p.isConnected()) {
                return true;
            }
        }
        // #sijapp cond.end #
        // #sijapp cond.if target is "MIDP2" #
        // #sijapp cond.if modules_FILES="true"#
        if (!isCollapsible()) {
            return true;
        }
        // #sijapp cond.end #
        // #sijapp cond.end #
        return false;
    }

    
    /* Builds the main menu (visual list) */
    public void activateMainMenu() {
        updateMainMenu();
        mainMenu.setDefaultItemCode(MENU_STATUS);
        mainMenuView.show();
    }

    // #sijapp cond.if modules_XSTATUSES is "true" #
    private int getXStatusCount(Protocol protocol) {
        if (null != protocol.getXStatusInfo()) {
            return protocol.getXStatusInfo().getXStatusCount();
        }
        return 0;
    }
    // #sijapp cond.end #
    
    private static final ImageList menuIcons = ImageList.createImageList("/menuicons.png"); //menu icons
    
    public void protocolMenu(MenuModel mainMenu, Protocol protocol) {
        activeProtocol = protocol;
        if (protocol.isConnecting()) {
            mainMenu.addItem("disconnect", menuIcons.iconAt(2),  MENU_DISCONNECT); //menu icons
            return;
        }
        if (protocol.isConnected()) {
            mainMenu.addItem("disconnect",  menuIcons.iconAt(2),  MENU_DISCONNECT); //menu icons
            
        } else {
            mainMenu.addItem("connect",  menuIcons.iconAt(1),  MENU_CONNECT); //menu icons
        }
        mainMenu.addItem("set_status",
                protocol.getStatusInfo().getIcon(protocol.getProfile().statusIndex),
                MENU_STATUS);
        // #sijapp cond.if modules_XSTATUSES is "true" #
        if (0 < getXStatusCount(protocol)) {
            Icon icon = protocol.getXStatusInfo().getIcon(protocol.getProfile().xstatusIndex);
            mainMenu.addItem("set_xstatus", icon, MENU_XSTATUS);
        }
        // #sijapp cond.end #
        // #sijapp cond.if protocols_ICQ is "true" #
        if (protocol instanceof Icq) {
            final Icq icq = (Icq)protocol;
            // #sijapp cond.if modules_SERVERLISTS is "true" #
            mainMenu.addItem("private_status", PrivateStatusForm.getIcon(protocol),
                    MENU_PRIVATE_STATUS);
            // #sijapp cond.end #
        }
        // #sijapp cond.end #
        if (protocol.isConnected()) {
            boolean hasVCard = true;
            // #sijapp cond.if protocols_JABBER is "true" #
            if (protocol instanceof Jabber) {
                hasVCard = ((Jabber)protocol).hasVCardEditor();
                if (((Jabber)protocol).hasS2S()) {
                    mainMenu.addItem("service_discovery", menuIcons.iconAt(3), MENU_DISCO); //menu icons
                }
            }
            // #sijapp cond.end #
            mainMenu.addItem("manage_contact_list", menuIcons.iconAt(4), MENU_GROUPS); //menu icons
            if (hasVCard) {
                mainMenu.addItem("myself", menuIcons.iconAt(5), MENU_MYSELF); //menu icons
            }
            // #sijapp cond.if protocols_MRIM is "true" #
            if (protocol instanceof Mrim) { 
                mainMenu.addItem("microblog", 
                        ((Mrim)protocol).getMicroBlog().getIcon(), MENU_MICROBLOG);
            }
            // #sijapp cond.end #
        }
    }
    public void updateMainMenu() {
        int currentCommand = mainMenuView.getSelectedItemCode();
        mainMenu.clean();
        mainMenu.addItem("keylock_enable",  menuIcons.iconAt(0),  MENU_KEYLOCK); //menu icons
        if (0 < getManager().getModel().getProtocolCount()) {
            protocolMenu(mainMenu, getManager().getCurrentProtocol());
            // #sijapp cond.if modules_MULTI is "true" #
            getManager().getCurrentProtocol().getProtocolBranch().updateMenu();
            // #sijapp cond.end #
        }
        if (isSmsSupported()) {
            mainMenu.addItem("send_sms", menuIcons.iconAt(6), MENU_SEND_SMS); //menu icons
        }
        mainMenu.addItem("options_lng", menuIcons.iconAt(7), MENU_OPTIONS); //menu icons
        
        // #sijapp cond.if modules_SOUND is "true" #
        if (Options.getBoolean(Options.OPTION_SILENT_MODE)) {            //menu icons
        mainMenu.addItem("#sound_on", menuIcons.iconAt(8), MENU_SOUND);  //menu icons
        } else {                                                         //menu icons
        mainMenu.addItem("#sound_off", menuIcons.iconAt(9), MENU_SOUND); //menu icons
        }                                                                //menu icons
        // #sijapp cond.end#
        
        // #sijapp cond.if modules_MAGIC_EYE is "true" #
        mainMenu.addItem("magic eye", menuIcons.iconAt(10), MENU_MAGIC_EYE); //menu icons
        // #sijapp cond.end#
        mainMenu.addItem("about", menuIcons.iconAt(11), MENU_ABOUT); //menu icons
        // #sijapp cond.if modules_DEBUGLOG is "true" #
        mainMenu.addItem("debug log", menuIcons.iconAt(12), MENU_DEBUGLOG); //menu icons
        mainMenu.addItem("test", menuIcons.iconAt(12), MENU_TEST); //menu icons
        // #sijapp cond.end#        
        // #sijapp cond.if target is "MIDP2" #
        if (isCollapsible()) {
            mainMenu.addItem("minimize", menuIcons.iconAt(13), MENU_MINIMIZE); //menu icons
        }
        // #sijapp cond.end#
        mainMenu.addItem("exit", menuIcons.iconAt(14), MENU_EXIT); //menu icons

        mainMenu.setDefaultItemCode(currentCommand);
        mainMenuView.setModel(mainMenu);
        mainMenuView.update();
    }

    private void doExit(boolean anyway) {
        NativeCanvas.stopKeyRepeating();
        Jimm.getJimm().quit();
    }
    
    /* Command listener */
    public void textboxAction(InputTextBox box, boolean ok) {
        if ((box == passwordTextBox) && ok) {
            final Protocol protocol = activeProtocol;
            protocol.setPassword(passwordTextBox.getString());
            passwordTextBox.back();
            if (!StringConvertor.isEmpty(protocol.getPassword())) { 
                protocol.connect();
            }
        }
    }
    public void alertAction() {
        doExit(true);
    }

    private void execCommand(int cmd) {
        final Protocol proto = activeProtocol;
        switch (cmd) {
            case MENU_CONNECT:
                if (proto.isEmpty()) {
                    new AccountsForm().showAccountEditor(proto.getProfile());

                } else if (StringConvertor.isEmpty(proto.getPassword())) {
                    passwordTextBox = new InputTextBox().create("password", 32, TextField.PASSWORD);
                    passwordTextBox.setTextBoxListener(this);
                    passwordTextBox.show();

                } else {
                    contactList.restore();
                    proto.connect();
                }
                break;

            case MENU_DISCONNECT:
                proto.disconnect();
                Thread.yield();
                activate();
                break;

            case MENU_KEYLOCK:
                Jimm.lockJimm();
                break;

            case MENU_STATUS:
                new SomeStatusForm(proto).show();
                break;

                // #sijapp cond.if modules_XSTATUSES is "true" #
            case MENU_XSTATUS:
                // #sijapp cond.if protocols_ICQ is "true" #
                if (proto instanceof Icq) {
                    new IcqXStatusForm((Icq)proto).show();
                }
                // #sijapp cond.end #
                // #sijapp cond.if protocols_MRIM is "true" #
                if (proto instanceof Mrim) {
                    new MrimXStatusForm((Mrim)proto).show();
                }
                // #sijapp cond.end #
                // #sijapp cond.if protocols_JABBER is "true" #
                if (proto instanceof Jabber) {
                    new JabberXStatusForm((Jabber)proto).show();
                }
                // #sijapp cond.end #
                // #sijapp cond.if protocols_OBIMP is "true" #
                if (proto instanceof Obimp) {
                    new ObimpXStatusForm((Obimp)proto).show();
                }
                // #sijapp cond.end #
                break;
                // #sijapp cond.end #

                // #sijapp cond.if protocols_ICQ is "true" #
                // #sijapp cond.if modules_SERVERLISTS is "true" #
            case MENU_PRIVATE_STATUS:
                new PrivateStatusForm(proto).show();
                break;
                // #sijapp cond.end #
                // #sijapp cond.end #

                // #sijapp cond.if protocols_JABBER is "true" #
            case MENU_DISCO:
                ((Jabber)proto).getServiceDiscovery().showIt();
                break;
                // #sijapp cond.end #
                
                // #sijapp cond.if protocols_MRIM is "true" #
            case MENU_MICROBLOG:
                ((Mrim)proto).getMicroBlog().activate();
                updateMainMenu();
                break;
                // #sijapp cond.end #
                
            case MENU_OPTIONS:
                new OptionsForm().show();
                break;

            case MENU_ABOUT:
                updateAbout();
                aboutTextList.show();
                break;

            case MENU_GROUPS:
                new ManageContactListForm(proto).show();
                break;

            case MENU_MINIMIZE:
                /* Minimize Jimm (if supported) */
                contactList.restore();
                Jimm.setMinimized(true);
                break;

            // #sijapp cond.if modules_DEBUGLOG is "true" #
            case MENU_DEBUGLOG:
                DebugLog.activate();
                break;
            // #sijapp cond.end#

            // #sijapp cond.if modules_MAGIC_EYE is "true" #
            case MENU_MAGIC_EYE:
                MagicEye.activate();
                break;
            // #sijapp cond.end#
                
            // #sijapp cond.if modules_SOUND is "true" #
            case MENU_SOUND:
                Notify.getSound().changeSoundMode(false);
                updateMainMenu();
                break;
            // #sijapp cond.end#
                
            case MENU_MYSELF:
                proto.showUserInfo(proto.createTempContact(proto.getUserId(), proto.getNick()));
                break;
                
            case MENU_SEND_SMS:
                new SmsForm(null, null).show();
                break;

            // #sijapp cond.if modules_DEBUGLOG is "true" #
            case MENU_TEST:
                for (int i = 0; i < 10; ++i) {
                    long m = ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024);
                    new Alert("memory "+ m + "kb", 
                            "text" + i + "\nhj:)khgkgfnh\n\n\ngtfn\ngtfn\ngtfn\ngter44fn\n"
                            + "tssfn\ngttwfn\ngtftrn\ngtfn\ngtf54354n\ngt435fn\ng43554tfn\nmenjkhk\n"
                            + ":):D:( :) :) q*WRITE*", "", "back", null).show();
                }
                break;
            // #sijapp cond.end#

            case MENU_EXIT:
                doExit(false);
                break;
        }
    }
    public void select(Select select, MenuModel model, int cmd) {
        execCommand(cmd);
    }
}